import instance from "../request"
import type { Ref } from "vue"

// 登录接口的参数类型
interface IAdminLoginParams {
    password: string;
    username: string
}
// 接口的返回值的类型
interface IAdminLoginRes {
    tokenHead: string;
    token: string
}
// /admin/login 登录后返回token
// Promise<T>是返回值的类型
const adminLogin = (data: IAdminLoginParams): Promise<IAdminLoginRes> => {
    return instance.post("admin/login", data,
        // {
        //     headers: { "content-type": "application/x-www-form-urlencoded" },
        // }
    )
}
// /admin/info 获取当前登录用户信息

interface IAdminInfoRes {
    icon: string;
    menus: IMenuItem[];
    roles: string[];
    username: string
}
const getAdminInfo = (): Promise<IAdminInfoRes> => {
    return instance.get("admin/info")
}

// 退出登录 /admin/logout
// 参数类型
interface IAdminListParmas {
    keyword?: string | Ref<string>;
    pageNum?: number | Ref<number>;
    pageSize?: number | Ref<number>;
}
// 返回值里面数组的类型
interface IAdminListItem {
    createTime: string,
    email: string,
    icon: string,
    id: number,
    loginTime: string,
    nickName: string,
    note: string,
    password: string,
    status: number,
    username: string
}
// 返回值类型
interface IAdminListRes {
    list: IAdminListItem[],
    pageNum: number,
    pageSize: number,
    total: number,
    totalPage: number
}
const logout = () => {
    return instance.post("admin/logout")
}
// 用户列表  /admin/list
const getAdminList = (data?: IAdminListParmas): Promise<IAdminListRes> => {
    return instance.get("admin/list", {
        params: data
    })
}




// 用获取用户角色列表  /role/list
const getRoleList = (data?: IAdminListParmas): Promise<any> => {
    return instance.get("role/list", {
        params: data
    })
}
// 分配资源页面
const allocResource = (): Promise<any> => {
    return instance.get("resource/listAll")
}

// 菜单列表 menu/list
const getMenuList = (data?: any): Promise<any> => {
    return instance.get(`menu/list${data.parentId}`, {
        params: data
    })
}
// 资源列表 resource/list
const getResourceList = (data?: any): Promise<any> => {
    return instance.get(`resource/list`, {
        params: data
    })
}






// 修改指定用户信息
const adminUpdate = (data: IAdminListItem) => {
    return instance.post(`admin/update/${data.id}`, data)
}
// 添加用户
const adminRegister = (data: IAdminListItem): Promise<IAdminListItem> => {
    return instance.post(`admin/register`, data)
}

// 获取首页的数据
interface IAdminStatRes {
    saleMap: {}[];
    salePie: {}[];
}
const getAdminStat = (): Promise<IAdminStatRes> => {
    return instance.get("http://kumanxuan1.f3322.net:8360/admin/stat")
}





export { adminLogin, getAdminInfo, logout, getAdminList, getRoleList, adminUpdate, adminRegister, getAdminStat, getMenuList, allocResource, getResourceList }
export type {
    IAdminListItem,
    IAdminStatRes
}